<?php
/**
 * 数据字典管理
 */

namespace dragon\Support;

use dragon\Cache\TableCache;
use dragon\SysConfig;

class Dictionary extends TableCache
{
    public static $table = 'dictionary';

    // 数据转化
    public static function convert($dictType, $key) {
        return static::get($dictType, $key);
    }

    // 键值获取
    public static function key($dictType, $val) {
        $all = static::get($dictType);
        return array_search($val, $all);
    }

    // 获取一类
    public static function get($dictType)
    {
        $all = static::all();

        if(func_num_args() == 1) return isset($all[$dictType]) ? $all[$dictType] : [];

        $key = func_get_arg(1);
        return isset($all[$dictType], $all[$dictType][$key]) ? $all[$dictType][$key] : [];
    }

    // 数据格式化
    public static function format($list)
    {
        $_list = [];
        foreach ($list as $val) {
            $_list[$val['dict_type']][$val['key']] = $val['value'];
        }
        return $_list;
    }
}